package com.ruoyi.tranShort.mqtt.request;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.mqtt.api.MqttClientApi;
import com.ruoyi.tranShort.mqtt.ResponseHandle;
import com.ruoyi.tranShort.mqtt.api.GatewayRequest;
import com.ruoyi.tranShort.mqtt.api.GatewayResponse;
import com.ruoyi.tranShort.mqtt.common.constants.ResponseConstants;
import com.ruoyi.tranShort.mqtt.common.constants.TypeConstants;
import com.ruoyi.tranShort.mqtt.entity.ComType;
import com.ruoyi.tranShort.mqtt.entity.Content;
import com.ruoyi.tranShort.mqtt.entity.DataApi;
import com.ruoyi.tranShort.mqtt.entity.RequestBase;
import com.ruoyi.tranShort.mqtt.exception.ResponseTimeOutException;
import com.ruoyi.tranShort.mqtt.request.entity.ConnectDeviceLongData;
import com.ruoyi.tranShort.mqtt.response.ConnectDeviceLongResponse;
import com.ruoyi.tranShort.utils.MqttClientUtil;
import org.apache.poi.openxml4j.opc.internal.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 长连接
 * @author wwy
 * @date 2021-08-10 14:49
 */
public class ConnectDeviceLongRequest implements GatewayRequest {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConnectDeviceLongRequest.class);
    private RequestBase requestBase = new RequestBase();
    private ComType comType = new ComType();
    private Content content = new Content();
    public ConnectDeviceLongRequest(){
        requestBase.setComType(comType);
        comType.setClientId("");
        comType.setMsgId(System.currentTimeMillis()+"");
        comType.setType(TypeConstants.DOWN);
        comType.setContent(content);
        content.setType(TypeConstants.CONNECT_DEVICE_LONG);

    }
    private ConnectDeviceLongData connectDeviceLongData;
    private String pubTopic;
    private int qos;
    @Override
    public ConnectDeviceLongResponse execute() {
        //初始化响应参数
        ConnectDeviceLongResponse connectDeviceLongResponse = new ConnectDeviceLongResponse();
        connectDeviceLongResponse.setCode(500);
        if(connectDeviceLongData != null){
            content.setData(connectDeviceLongData);
            //发送消息
            MqttClientApi mqttClientApi = MqttClientUtil.getMqttClientApi();
            String message = JSONObject.toJSONString(requestBase);
            LOGGER.info("长连接接口发送消息：{}",message);
            mqttClientApi.publishMessage(pubTopic,message,qos);
            //监听上行消息，判断是否成功
            String mac = connectDeviceLongData.getMac();

            try {
                String waitMessage = ResponseHandle.waitConnectMessage(mac.toUpperCase());
                if(ResponseConstants.OK.equals(waitMessage)){
                    connectDeviceLongResponse.setCode(200);
                }else{
                    connectDeviceLongResponse.setCode(500);
                }

            } catch (ResponseTimeOutException e) {
                //超时
                connectDeviceLongResponse.setCode(500);
            }
        }

        return connectDeviceLongResponse;
    }

    public ConnectDeviceLongData getConnectDeviceLongData() {
        return connectDeviceLongData;
    }

    public void setConnectDeviceLongData(ConnectDeviceLongData connectDeviceLongData) {
        this.connectDeviceLongData = connectDeviceLongData;
    }

    public String getPubTopic() {
        return pubTopic;
    }

    public void setPubTopic(String pubTopic) {
        this.pubTopic = pubTopic;
    }

    public int getQos() {
        return qos;
    }

    public void setQos(int qos) {
        this.qos = qos;
    }
}
